package com.example.project.projectspringcloud.service.impl.user;

import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.dao.user.EvaluateMapper;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.order.Order;
import com.example.project.projectspringcloud.entity.user.Evaluate;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.entity.user.pojo.EvaluatePojo;
import com.example.project.projectspringcloud.service.BaseService;
import com.example.project.projectspringcloud.service.order.OrderService;
import com.example.project.projectspringcloud.service.user.UserEvaluateService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.utils.WebUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Evaluate Service
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserEvaluateServiceImpl extends BaseService<Evaluate, EvaluateMapper> implements UserEvaluateService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Override
    public Evaluate get(Evaluate evaluate) {
        return super.mapper.selectOne(evaluate);
    }

    @Override
    public List<Evaluate> list(Page page, Evaluate evaluate) {
        List<Evaluate> evaluates = super.list(page, evaluate);
        evaluates.forEach(t -> {
            t.setEvaluateUser(userService.get(new User(t.getUserId())));
        });
        return evaluates;
    }

    @Override
    public boolean save(Evaluate evaluate) {
        return super.save(evaluate);
    }

    @Override
    public boolean delete(Integer[] ids) {
        return super.mapper.deleteByArrayPrimaryKey(ids) > 0;
    }

    @Override
    public boolean evaluate(EvaluatePojo pojo) throws OperationException {
        Date currentDate = new Date();
        User currentUser = WebUtil.getCurrentUser();

        // 取出订单原始数据
        Order order = orderService.get(new Order(pojo.getTarget()));
        // 已评价
        if (order != null && order.getStatusStep() != null && order.getStatusStep().equals(1)) {
            return false;
        }

        // 作品符合度
        Optional.ofNullable(pojo.getProductionConformity()).ifPresent(t -> {
            t.setTargetType(1);
            t.setUserId(currentUser.getId());
            t.setTarget(pojo.getTarget());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });
        // 总体满意度
        Optional.ofNullable(pojo.getSatisficing()).ifPresent(t -> {
            t.setTargetType(2);
            t.setUserId(currentUser.getId());
            t.setTarget(pojo.getTarget());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });
        // 客服
        Optional.ofNullable(pojo.getCustomerService()).ifPresent(t -> {
            t.setTargetType(3);
            t.setUserId(currentUser.getId());
            t.setTarget(pojo.getTarget());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });
        // 摄影师
        Optional.ofNullable(pojo.getPhotoGrapher()).ifPresent(t -> {
            t.setTargetType(4);
            t.setUserId(currentUser.getId());
            t.setTarget(order.getOrderDetail().getPhotographer());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });
        // 编剧
        Optional.ofNullable(pojo.getScriptWriter()).ifPresent(t -> {
            t.setTargetType(5);
            t.setUserId(currentUser.getId());
            t.setTarget(order.getOrderDetail().getScriptwriter());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });
        // 作品
        Optional.ofNullable(pojo.getProduction()).ifPresent(t -> {
            t.setTargetType(6);
            t.setUserId(currentUser.getId());
            // 对作品评价，如果是同款，评价的是原作品，如果是私人订制，评价的是作品本身
            t.setTarget(order.getOrderDetail().getSameStyleId() != null ? order.getOrderDetail().getSameStyleId() : pojo.getTarget());
            t.setAnonymous(pojo.isAnonymous() ? 1 : 0);
            t.setCreateDate(currentDate);
            save(t);
        });

        // 更新订单已完成下级状态为已评价
        order.setStatusStep(1);
        orderService.save(order);

        return true;
    }
}
